from study321.menu.entry.user import UserInfo
class UserService(object):
    users = []
    def __select_user_by_name(self,name):
        flag = False
        for user in UserService.users:
            if user.get_name() == name:
                flag = True
                return user

        if not flag:
            return None

    def register_user(self):
        name = input("请输入你的用户名：")
        password = input("请输入你的密码：")
        repass = input("请再次输入你的密码：")
        if password == repass:
            user = UserInfo(name=name, password=password)
            # 判断一下用户是否存在
            res = self.__select_user_by_name(name)
            if res is None:
                UserService.users.append(user)
                print("注册成功")
            else:
                print("当前用户已经存在，请勿重复注册")
        else:
            print("两次密码不一致，请重新注册")

    def login(self):
        name = input("请输入用户名：")
        password = input("请输入密码：")
        user = self.__select_user_by_name(name)
        if user is None:
            print("暂无该用户，登录失败")
            return 0
        else:
            if user.get_password() == password:
                return 1
            else:
                print("密码不正确")
                return 0